package com.lanou.demo.backgrounddemo.controller;

import com.lanou.demo.backgrounddemo.service.ItemParamService;

import com.lanou.entity.entity.ItemParamEntity;
import com.lanou.entity.utils.AjaxResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 常宏盛
 * Date: 2019/7/11 20:51
 **/
@RestController
@RequestMapping("/item/param")
public class ItemParamController {

    @Resource
    private ItemParamService itemParamService;

    /**
     * 规格参数分页查询
     *
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult list(int page, int rows) {
        int count = this.itemParamService.count();
        List<ItemParamEntity> itemParamList = this.itemParamService.list(page, rows);
        return AjaxResult.builder().total(count).rows(itemParamList).build();
    }

    /**
     * 判断选择的类目是否已经添加过规格
     *
     * @param id
     * @return
     */
    @RequestMapping("/query/itemcatid/{id}")
    public AjaxResult queryParamDataByCid(@PathVariable Long id) {
        ItemParamEntity itemParam = this.itemParamService.queryParamDataByCid(id);
        if (itemParam == null) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).data(itemParam).build();
    }

    /**
     * 规格参数新增
     *
     * @param cid
     * @param paramData
     * @return
     */
    @RequestMapping("/save/{cid}")
    public AjaxResult save(@PathVariable Long cid, String paramData) {
        int saveResult = this.itemParamService.save(cid, paramData);
        if (saveResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

    /**
     * 规格参数删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public AjaxResult delete(String[] ids) {
        int deleteResult = this.itemParamService.delete(ids);
        if (deleteResult == 0) {
            return AjaxResult.builder().status(400).build();
        }
        return AjaxResult.builder().status(200).build();
    }

}
